package slstone.user.login.cmd;

import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import slstone.user.login.biz.LogInBiz;
import slstone.user.login.vo.LogInVO;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class LogInCmd extends ActionSupport implements ModelDriven<LogInVO>, SessionAware, RequestAware
{

	private LogInVO logInVO;
	private LogInBiz logInBiz;
	private Map<String, Object> session;
	private Map<String, Object> request;

	@Override
	public String execute() throws Exception
	{
				// 사용자 정보 검증 메소드 호출전 화면에서 받은 데이터에 대해서 처리를 한다(앞뒤 공백부분은 빼줌)
				String userId = this.logInVO.getUserId().trim();
				String password = this.logInVO.getPassword().trim();

				this.logInVO.setUserId(userId);
				this.logInVO.setPassword(password);

				// 사용자 정보 검증 메소드 호출
				String massage = logInBiz.logInValidate(this.logInVO);


				if ("ok".equals(massage))
				{
					// 사용자 ID를 세션에 저장한다
					this.session.put("userId", this.logInVO.getUserId());

					return "success";
				}
				else
				{

					request.put("userId", userId);
					request.put("password", password);
					request.put("massage", massage);

					// 获取request 方法1
					/*
					 * HttpServletRequest request =
					 * ServletActionContext.getRequest();
					 * request.setAttribute("massage", massage);
					 */

					// 获取request 方法2
					/*
					 * ActionContext context = ActionContext.getContext();
					 * Map<String, Object> requestMap = (Map<String,
					 * Object>)context.get("request");
					 */
					
					return "input";
				}						
	}

	/*********************************************************************************/
	
	
	public LogInBiz getLogInBiz()
	{
		return logInBiz;
	}

	public LogInVO getLogInForm()
	{
		return logInVO;
	}

	public void setLogInForm(LogInVO logInVO)
	{
		this.logInVO = logInVO;
	}

	public void setLogInBiz(LogInBiz logInBiz)
	{
		this.logInBiz = logInBiz;
	}

	@Override
	public LogInVO getModel()
	{
		return logInVO;
	}

	@Override
	public void setSession(Map<String, Object> session)// session 획득
	{
		this.session = session;
	}

	@Override
	public void setRequest(Map<String, Object> request)// request 획득
	{
		this.request = request;

	}


}
